package com.cxm.helper.config.web;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.stp.StpUtil;
import com.cxm.helper.exception.BusinessException;
import com.cxm.helper.util.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: chenximeng
 * @date: 2022/11/18 10:06
 */
@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<String> businessExceptionHandler(BusinessException e) {
        log.warn("捕获业务异常信息", e);
        return ResponseEntity.fail(500, e.getMessage());
    }

    @ExceptionHandler(NotRoleException.class)
    public ResponseEntity<String> notRoleExceptionHandler(NotRoleException e) {
        log.error("没有权限,id=" + StpUtil.getLoginIdAsString(), e);
        return ResponseEntity.fail(403, "没有权限, 请联系管理员");
    }

    @ExceptionHandler(NotLoginException.class)
    public ResponseEntity<String> notLoginExceptionHandler(NotLoginException e) {
        log.error("未登录或token过期", e);
        return ResponseEntity.fail(403, "未登录或token过期了, 请重新登陆");
    }

    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity<String> nullPointerExceptionHandler(NullPointerException e) {
        log.warn("捕获业务异常信息", e);
        return ResponseEntity.fail(500, "空指针了");
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> exceptionHandler(Exception e) {
        log.error("捕获异常信息", e);
        return ResponseEntity.fail(500, "发现异常" + e.getMessage());
    }



}
